package cn.shengchao.job.gankao;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.shengchao.examstar.gankao.acl.ports.repositories.IGanKaoRepository;
import cn.shengchao.examstar.gankao.infrastrure.po.GkSyncTask;
import cn.shengchao.examstar.product.acl.adapters.repositories.SpecificationTypeRepository;
import cn.shengchao.examstar.product.infrastructure.dao.po.TSpecificationType;
import cn.shengchao.gankao.GanKaoDomainService;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.common.log.SnailJobLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


/**
 * @program: examstarapi
 * @description: 赶考任务的加载处理
 * @author: Xiong weiqiang
 * @create: 2025-06-23 17:22
 */
@Component
public class GanKaoTaskProcessJob {
    @Autowired
    private IGanKaoRepository ganKaoRepository;
    @Autowired
    private GanKaoDomainService ganKaoDomainService;
    @Autowired
    private SpecificationTypeRepository specificationTypeRepository;
    @JobExecutor(name = "GanKaoTaskCreateJob")
    public void processGanKaoTaskCreateJob(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始创建赶考网同步数据到本地赶考课程任务" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        ganKaoRepository.createLoadVersion(startTime);
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始创建赶考网同步数据到本地赶考课程任务：，耗时："+(endTime-startTime)+"毫秒");
    }
    @JobExecutor(name = "GanKaoTaskSyncSpecTypeJob")
    public void processGanKaoTaskSyncSpecTypeJob(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始处理赶考网科目数据创建本地赶考课程类目任务" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        String params=jobArgs.getJobParams().toString();
        if (StrUtil.isNotBlank(params)&& JSONUtil.isTypeJSON( params)){
            Long specTypeId= JSONUtil.parseObj( params).getLong("spec_type_id");
            if (specTypeId!=null){
                //731114185517187106L
                List<TSpecificationType> list=specificationTypeRepository.getSpecificationTypesByIdList(List.of(specTypeId));
                if (CollUtil.isNotEmpty( list)){
                    SnailJobLog.REMOTE.info("开始处理赶考网科目数据到本地赶考课程类目任务：，目录："+list.getFirst().toString());
                    ganKaoDomainService.syncGrade(specTypeId);
                }
            }
        }
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始处理赶考网科目数据到本地赶考课程类目任务：，耗时："+(endTime-startTime)+"毫秒");
    }
    @JobExecutor(name = "GanKaoTaskSyncCourseType2ToCourseKnowledgeJob")
    public void processGanKaoTaskSyncCourseType2ToCourseKnowledgeJob(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始处理赶考知识点资源课程2生成本地课程知识点任务" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        ganKaoDomainService.syncCourseType2ToLocalCourse();
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始处理赶考知识点资源课程2生成本地课程知识点任务：，耗时："+(endTime-startTime)+"毫秒");
    }


    @JobExecutor(name = "GanKaoTaskSyncCourseType1ToCourseKnowledgeJob")
    public void processGanKaoTaskSyncCourseType1ToCourseKnowledgeJob(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始处理赶考同步课程1生成本地课程知识点任务" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        ganKaoDomainService.syncCourseType1ToLocalCourse();
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始处理赶考同步课程1生成本地课程知识点任务：，耗时："+(endTime-startTime)+"毫秒");
    }

    @JobExecutor(name = "GanKaoTaskSyncCourseSpecialTagToCourseKnowledgeJob")
    public void processGanKaoTaskSyncCourseToCourseKnowledgeJob(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始处理赶考网特殊标签课程生成本地课程知识点任务" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        String params=jobArgs.getJobParams().toString();
        if (StrUtil.isNotBlank(params)&& JSONUtil.isTypeJSON( params)){
            String specTag= JSONUtil.parseObj( params).getStr("spec_tag");
            if (specTag!=null){
                ganKaoDomainService.syncCourseTagToLocalCourse(specTag);
            }
        }
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始处理赶考网特殊标签课程生成本地课程知识点任务：，耗时："+(endTime-startTime)+"毫秒");
    }
    @JobExecutor(name = "GanKaoTaskLoadSpecialCourseJob")
    public void GanKaoTaskLoadSpecialCourseJob(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始处理赶考网特殊标签课程到本地赶考课程任务" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        GkSyncTask gkSyncTask=ganKaoRepository.findNeedLoadTask();
        if (gkSyncTask!=null){
            try {
                ganKaoDomainService.requestSpecialCourseByLoadVersion(gkSyncTask.getLoadVersion());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else {
            SnailJobLog.REMOTE.info("开始处理赶考网特殊标签课程到本地赶考课程任务:" + "没有需要处理的任务");
        }
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始处理赶考网特殊标签课程到本地赶考课程任务：，耗时："+(endTime-startTime)+"毫秒");
    }

    @JobExecutor(name = "GanKaoTaskLoadDataJob")
    public void processGanKaoTaskLoadDataJob(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始处理赶考网同步数据到本地赶考课程任务" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        GkSyncTask gkSyncTask=ganKaoRepository.findNeedLoadTask();
        if (gkSyncTask!=null){
            try {
                ganKaoDomainService.requestDataByLoadVersion(gkSyncTask.getLoadVersion());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else {
            SnailJobLog.REMOTE.info("开始处理赶考网同步数据到本地赶考课程任务:" + "没有需要处理的任务");
        }
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始处理赶考网同步数据到本地赶考课程任务：，耗时："+(endTime-startTime)+"毫秒");
    }
    @JobExecutor(name = "GanKaoTaskSyncCourseDetailJob")
    public void processGanKaoTaskSyncCourseDetailJob(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始处理本地赶考加载所有课程详情任务：" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        GkSyncTask gkSyncTask=ganKaoRepository.findNeedLoadDetailTask();
        if (gkSyncTask!=null){
            try {
                ganKaoDomainService.requestDataByGetCourseDetail(gkSyncTask.getLoadVersion());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else {
            SnailJobLog.REMOTE.info("开始处理本地赶考加载所有课程详情任务:" + "没有需要处理的任务");
        }
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始处理本地赶考加载所有课程详情任务：耗时："+(endTime-startTime)+"毫秒");
    }
    @JobExecutor(name = "GanKaoTaskSyncCourseDetailForSpecialJob")
    public void processGanKaoTaskSyncCourseDetailForSpecialJob(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始处理赶考特殊标签课程加载详情任务：" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        String params=jobArgs.getJobParams().toString();
        if (StrUtil.isNotBlank(params)&& JSONUtil.isTypeJSON( params)){
            String specTag= JSONUtil.parseObj( params).getStr("spec_tag");
            GkSyncTask gkSyncTask=ganKaoRepository.findNeedLoadDetailTask();
            if (specTag!=null&& gkSyncTask!=null){
                ganKaoDomainService.requestDataByGetCourseDetailForSpecialTag(gkSyncTask.getLoadVersion(),specTag);
            }else {
                SnailJobLog.REMOTE.info("开始处理赶考特殊标签课程加载详情任务:" + "没有需要处理的任务");
            }
        }else {
            SnailJobLog.REMOTE.info("开始处理赶考特殊标签课程加载详情任务:" + "没有需要处理的任务");
        }
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始处理赶考特殊标签课程加载详情任务：耗时："+(endTime-startTime)+"毫秒");
    }
    @JobExecutor(name = "GanKaoTaskSyncCourseDetailForCourseId")
    public void processGanKaoTaskSyncCourseDetailForCourseId(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始处理指定课程id赶考课程加载详情任务：" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        String params=jobArgs.getJobParams().toString();
        if (StrUtil.isNotBlank(params)&& JSONUtil.isTypeJSON( params)){
            JSONArray courseIds= JSONUtil.parseObj( params).getJSONArray("courseIds");
            GkSyncTask gkSyncTask=ganKaoRepository.findNeedLoadDetailTask();
            if (CollUtil.isNotEmpty(courseIds)&& gkSyncTask!=null){
                ganKaoDomainService.requestCourseDetailByCourseIds(gkSyncTask.getLoadVersion(),courseIds.toList(Integer.class));
            }else {
                SnailJobLog.REMOTE.info("开始处理指定课程id赶考课程加载详情任务:" + "没有需要处理的任务");
            }
        }else {
            SnailJobLog.REMOTE.info("开始处理指定课程id赶考课程加载详情任务:" + "没有需要处理的任务");
        }
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始处理指定课程id赶考课程加载详情任务：耗时："+(endTime-startTime)+"毫秒");
    }


    @JobExecutor(name = "GanKaoTaskSyncAddCourseSpecTypeAndSetting")
    public void processGanKaoTaskSyncAddCourseSpecTypeAndSetting(JobArgs jobArgs) {
        SnailJobLog.REMOTE.info("开始处理本地赶考课程的多类目和有效期设置任务：" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        String params=jobArgs.getJobParams().toString();
        if (true){
            ganKaoDomainService.syncGanKaoCourseTagProductCourseSpecTypeRelation();
        }else {
            SnailJobLog.REMOTE.info("开始处理本地赶考课程的多类目和有效期设置任务:" + "没有需要处理的任务");
        }
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("开始处理本地赶考课程的多类目和有效期设置任务：耗时："+(endTime-startTime)+"毫秒");
    }


    @JobExecutor(name = "GanKaoTesEnvJob")
    public void processGanKaoTesEnvJob(JobArgs jobArgs) {
        if (jobArgs.getJobParams()!=null&&StrUtil.isNotBlank(jobArgs.getJobParams().toString())){

        }
        SnailJobLog.REMOTE.info("测试赶考网环境任务" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        long startTime = System.currentTimeMillis();
        ganKaoDomainService.testEnv();
        long endTime = System.currentTimeMillis();
        SnailJobLog.REMOTE.info("测试赶考网环境任务：，耗时："+(endTime-startTime)+"毫秒");
    }
}